// pages/activity-writeoff-confirm/activity-writeoff-confirm.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this;
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      console.log(scene)
      let id = scene.split("=")[1];
      console.log(id)
      that.setData({
        id,
      },()=>{
        that.getActiveOrderDetail(); //获取活动详情
      })
    }
    
  },

  getActiveOrderDetail() { //获取活动详情
    var that = this;
    wx.request({
      url: app.globalData.host + 'active/getActiveOrderDetail',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        id: that.data.id,
        user_id: wx.getStorageSync('userId')
      },
      success(res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          activity: res.data.data,
        })
      },
      fail(err) { },
      complete(comp) { },
    })
  },

  confirmTap() { //确定签到
    var that = this;
    var id = that.data.id;
    app.confirmLogin(
      function() {
        wx.showModal({
          title: '提示',
          content: '确认签到吗？',
          success(res) {
            if (res.confirm) {

              wx.request({
                url: app.globalData.host + 'active/activeSign',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                method: "POST",
                data: {
                  active_id: id,
                  user_id: wx.getStorageSync('userId')
                },
                success(res) {
                  console.log(res)
                  if(res.data.code==1){ //核销成功
                    wx.redirectTo({
                      url: '/pages/activity-writeoff-finish/activity-writeoff-finish?id=' + id,
                    })
                  }else{
                    wx.showToast({
                      title: res.data.msg,
                      icon: 'none',
                      duration: 2000,
                      mask: true,
                    })
                  }
                },
                fail(err) {},
                complete(comp) {},
              })




            }
          },
        })

      })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})